<?php

	require '../slimloader/Slim/Slim.php';
	
	require 'helpers/DbHelper.php';
	require 'helpers/GeneralUtil.php';
	require 'helpers/Config.php';
	\Slim\Slim::registerAutoloader();
	$app = new \Slim\Slim(array(
		'debug'=>true
	));
	$app->get('/jobs/:lat/:long/:distance/:offset/:apikey', function ($lat,$long,$distance,$offset,$apikey) use ($config) { 
		if($apikey!=$config['devapipassword']){
			echo json_encode(generateError('0','Miss Match API Key'));
			exit;
		}
		if($lat!="" && $long!=""){
			$respData=array();
			$dbHelper =new DbHelper();
					$con = mysqli_connect($config['database']['host'],$config['database']['username'],$config['database']['password'],$config['database']['table']);

			//$con = mysqli_connect("localhost","root","--327yqz","worktive");
			if (mysqli_connect_errno()){
				$respData=generateError('0',"Failed to connect to MySQL: " . mysqli_connect_error());
			}else{
				if($offset==null || $offset==""){
					$offset=0;
				}
				$result=$dbHelper->getLocationIdByLatLng($con,$lat,$long,$distance,$offset);
				$respData=buildOutJobResults($result,$dbHelper,$con,$offset);
			}
			echo json_encode($respData);
		}
	});
	$app->run();
?>